layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 评论管理
     */
    var FlowMemberComment = {
        tableId: "flowMemberCommentTable"
    };

    /**
     * 初始化表格的列
     */
    FlowMemberComment.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'commentId', hide: true, title: 'ID'},
            {field: 'memberName', sort: true, title: '会员姓名'},
            {field: 'city', sort: true, title: '服务城市'},
            {field: 'memberPhone', sort: true, title: '会员电话'},
            {field: 'note', sort: true, title: '评价内容'},
            {field: 'start', sort: true, title: '分数'},
            {field: 'typeName', sort: true, title: '服务类型'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'imgUrls', sort: true, title: '评价图片',templet:function (row) {
                if(!row.imgUrls){
                    return "无图";
                }else {
                    var imgs = row.imgUrls.split(",");
                    var result = "";
                    for (var i = 0;i<imgs.length;i++){
                        result += "<img src='"+imgs[i]+"' width='120' height='120' />";
                    }
                    return result;
                }
            }},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowMemberComment.search = function () {
        var queryData = {};

        queryData['memberName'] = $('#memberName').val();
        queryData['memberPhone'] = $('#memberPhone').val();

        table.reload(FlowMemberComment.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    FlowMemberComment.openAddDlg = function () {
        func.open({
            title: '添加评论',
            content: Feng.ctxPath + '/flowMemberComment/add',
            tableId: FlowMemberComment.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      FlowMemberComment.openEditDlg = function (data) {
          func.open({
              title: '修改评论',
              content: Feng.ctxPath + '/flowMemberComment/edit?commentId=' + data.commentId,
              tableId: FlowMemberComment.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    FlowMemberComment.exportExcel = function () {
        var checkRows = table.checkStatus(FlowMemberComment.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowMemberComment.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowMemberComment/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FlowMemberComment.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("commentId", data.commentId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowMemberComment.tableId,
        url: Feng.ctxPath + '/flowMemberComment/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowMemberComment.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FlowMemberComment.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    FlowMemberComment.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        FlowMemberComment.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FlowMemberComment.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FlowMemberComment.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FlowMemberComment.onDeleteItem(data);
        }
    });
});
